package com.ujoin.dao.imp;

import java.sql.Timestamp;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ujoin.bean.Invitation;
import com.ujoin.dao.IInvitationDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * Invitation entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.ujoin.bean.Invitation
 * @author MyEclipse Persistence Tools
 */

public class InvitationDAO extends HibernateDaoSupport implements IInvitationDAO {
	private static final Logger log = LoggerFactory
			.getLogger(InvitationDAO.class);
	// property constants
	public static final String ACTIVITY_ID = "activityId";
	public static final String INVITER_ID = "inviterId";
	public static final String INVITEE_ID = "inviteeId";
	public static final String IS_TREATED = "isTreated";
	public static final String STATUS = "status";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IInvitationDAO#save(com.ujoin.bean.Invitation)
	 */
	@Override
	public void save(Invitation transientInstance) {
		log.debug("saving Invitation instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IInvitationDAO#delete(com.ujoin.bean.Invitation)
	 */
	@Override
	public void delete(Invitation persistentInstance) {
		log.debug("deleting Invitation instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IInvitationDAO#findById(java.lang.Integer)
	 */
	@Override
	public Invitation findById(java.lang.Integer id) {
		log.debug("getting Invitation instance with id: " + id);
		try {
			Invitation instance = (Invitation) getHibernateTemplate().get(
					"com.ujoin.bean.Invitation", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IInvitationDAO#findByExample(com.ujoin.bean.Invitation)
	 */
	@Override
	public List findByExample(Invitation instance) {
		log.debug("finding Invitation instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IInvitationDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Invitation instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Invitation as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IInvitationDAO#findByActivityId(java.lang.Object)
	 */
	@Override
	public List findByActivityId(Object activityId) {
		return findByProperty(ACTIVITY_ID, activityId);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IInvitationDAO#findByInviterId(java.lang.Object)
	 */
	@Override
	public List findByInviterId(Object inviterId) {
		return findByProperty(INVITER_ID, inviterId);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IInvitationDAO#findByInviteeId(java.lang.Object)
	 */
	@Override
	public List findByInviteeId(Object inviteeId) {
		return findByProperty(INVITEE_ID, inviteeId);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IInvitationDAO#findByIsTreated(java.lang.Object)
	 */
	@Override
	public List findByIsTreated(Object isTreated) {
		return findByProperty(IS_TREATED, isTreated);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IInvitationDAO#findByStatus(java.lang.Object)
	 */
	@Override
	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IInvitationDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all Invitation instances");
		try {
			String queryString = "from Invitation";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IInvitationDAO#merge(com.ujoin.bean.Invitation)
	 */
	@Override
	public Invitation merge(Invitation detachedInstance) {
		log.debug("merging Invitation instance");
		try {
			Invitation result = (Invitation) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IInvitationDAO#attachDirty(com.ujoin.bean.Invitation)
	 */
	@Override
	public void attachDirty(Invitation instance) {
		log.debug("attaching dirty Invitation instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IInvitationDAO#attachClean(com.ujoin.bean.Invitation)
	 */
	@Override
	public void attachClean(Invitation instance) {
		log.debug("attaching clean Invitation instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IInvitationDAO getFromApplicationContext(ApplicationContext ctx) {
		return (IInvitationDAO) ctx.getBean("InvitationDAO");
	}
}